package com.wt.struggle.goodbye.message

import android.app.Activity
import android.os.Bundle
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import com.google.gson.reflect.TypeToken
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.Config
import com.wt.struggle.goodbye.base.ProActivity
import com.wt.struggle.goodbye.base.Send
import com.wt.struggle.goodbye.info.MessageInfo
import com.wt.struggle.goodbye.info.NetInfo
import com.wt.struggle.goodbye.main.adapter.CommentAdapter
import com.wt.struggle.goodbye.main.adapter.TextAdapter
import com.xin.lv.yang.utils.utils.DataUtil
import com.xin.lv.yang.utils.utils.ImageUtil
import kotlinx.android.synthetic.main.head.*
import kotlinx.android.synthetic.main.message_details_layout.*
import kotlinx.android.synthetic.main.text_item_layout.view.*
import org.json.JSONObject

class MessageDetailsActivity : ProActivity() {

    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {

        when (what) {
            Send.GET_MESSAGE_CODE -> {
                if (netInfo.code == Config.SUCCESS) {

                    val data = netInfo.dataResult
                    if (data != "") {
                        val json = JSONObject(data)

                        val sonList = json.optString("subMessageInfoList")

                        if (sonList != null && sonList != "") {

                            val arr = gson!!.fromJson<ArrayList<MessageInfo>>(
                                sonList,
                                object : TypeToken<ArrayList<MessageInfo>>() {}.type
                            )

                            if (arr != null && arr.size != 0) {
                                show(arr)
                            }

                        }

                    }

                }
            }
        }

    }


    fun show(arr: ArrayList<MessageInfo>) {

        commentLinear.removeAllViews()

        for (temp in arr) {

            val vvv = layoutInflater.inflate(R.layout.text_item_layout, null)
            val commentItem = vvv.commentItem
            commentItem.text = temp.content

            commentLinear.addView(vvv)
        }
    }


    private fun showDetails(newsInfo: MessageInfo?) {

        tvMessageMs.text = DataUtil.getEmoji(this, newsInfo!!.content)

        val time = newsInfo.createTime

        tvMessageSendTime.text = DataUtil.longToTime(time!!.toLong() / 1000, "yyyy-MM-dd")

        if (newsInfo.fromUserInfo != null) {
            msgName.text = newsInfo.fromUserInfo!!.nickName

            ImageUtil.getInstance()
                .loadCircleImage(
                    this,
                    msgHead,
                    Config.GET_IMAGE_URL + newsInfo.fromUserInfo!!.avatar,
                    R.mipmap.logo_small
                )
        }

    }

    var messageInfo: MessageInfo? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.message_details_layout)

        tvTitle.text = "消息详情"

        imageBack.setOnClickListener {
            setResult(Activity.RESULT_OK)
            finish()
        }


        messageInfo = intent.getParcelableExtra<MessageInfo>("messageId")

        showDetails(messageInfo)

        getMessageDetails(messageInfo!!.id!!)

    }


    /**
     * 改变消息状态
     */
    private fun getMessageDetails(messageId: Int) {
        send!!.getMessageDetails(messageId, handler)

    }
}